<template>
  <div class="dndList">
    <div :style="{width:width1}" class="dndList-list">
      <h3>{{ list1Title }}</h3>
      <div class="item-box">
        <draggable group="article"
                   class="dragArea"
                   v-model="list3">
          <el-tag
            v-for="tag in list3"
            :key="tag.name"
            closable
            @close="deleteEle(tag)"
            class="checked-item"
            type="success">
            {{ tag.name }}
          </el-tag>
        </draggable>
      </div>
    </div>
    <div :style="{width:width2}" class="dndList-list">
      <el-input v-model="searchText"
                prefix-icon="el-icon-search"
                placeholder="输入点位名称进行过滤"
                class="position-search"/>
      <div class="item-box">
        <el-tag v-for="(element) in list2"
                @click="pushEle(element)"
                style="display: block;cursor: pointer;margin-top: 5px;width: 70%">{{ element.name }}
        </el-tag>
      </div>

    </div>
  </div>
</template>

<script>
import draggable from 'vuedraggable'

export default {
  name: 'DndList',
  components: {draggable},
  data() {
    return {
      searchText: '',
      allList2: [],
      list3: []
    }
  },
  watch: {
    searchText: function (newValue, oldValue) {
      this.list2 = this.allList2.filter(item => {
        return item.name.indexOf(newValue) != -1
      })
    },
    list1:function (newValue){
      this.list3=newValue
    },
    list3:function (){
      let arr=this.list3.map(item=>{
        return {id:item.id,name:item.name}
      })
      this.$emit('update:result',JSON.stringify(arr))
    }
  },
  props: {
    list1: {
      type: Array,
      default() {
        return []
      }
    },
    list2: {
      type: Array,
      default() {
        return []
      }
    },
    list1Title: {
      type: String,
      default: 'list1'
    },
    result: {
      type: String,
      default: ''
    },
    width1: {
      type: String,
      default: '40%'
    },
    width2: {
      type: String,
      default: '55%'
    }
  },
  methods: {
    isNotInList1(v) {
      return this.list1.every(k => v.id !== k.id)
    },
    deleteEle(ele) {
      for (const item of this.list3) {
        if (item.id === ele.id) {
          const index = this.list3.indexOf(item)
          this.list3.splice(index, 1)
          break
        }
      }
    },
    pushEle(ele) {
      if (this.isNotInList1(ele)) {
        this.list3.push(ele)
      } else {
        this.$message.warning('该巡更点已添加过了!')
      }
    }
  },
  created() {
    this.allList2 = Object.assign([], this.list2)
    this.list3=Object.assign([],this.list1)
  },
}
</script>

<style lang="scss" scoped>
.dndList {
  background: #fff;
  padding-bottom: 20px;
  min-width: 800px;
  overflow: auto;

  h3 {
    font-size: 12px;
    font-weight: normal;
    border-bottom: 1px solid #cccccc;
    color: orange;
    font-style: italic;
    height: 30px;
    line-height: 30px;
    margin: 0;
  }

  .checked-item {
    display: block;
    cursor: move;
    margin-top: 5px;
    width: 50%;
  }

  &:after {
    content: "";
    display: table;
    clear: both;
  }

  .dndList-list {
    float: left;
    padding: 5px;
    border: 1px solid #cccccc;
    border-radius: 5px;

    .item-box {
      height: 300px;
      overflow: auto;
    }

    &:first-of-type {
      margin-right: 5%;
    }

    .dragArea {
      margin-top: 15px;
      min-height: 50px;
      padding-bottom: 30px;
    }
  }
}

</style>
